using UnityEngine;
using System.Collections;

public class playerManager : MonoBehaviour {
	public bool p1 = false, p2 = false, p3 = false, p4 = false, isLoading = false;
	public int pNum = 0;
	// Use this for initialization
	void Start () {
		DontDestroyOnLoad(transform.gameObject);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	public void addPlayer(int i){
		networkView.RPC("playerManagerAddPlayer",RPCMode.All,i);
	}
	void OnNetworkLoadedLevel(){
		isLoading = true;
		print("Syncing");
		networkView.RPC("playerManagerRequestSync",RPCMode.Server);	
	}
	[RPC]
	void playerManagerRequestSync(){
		networkView.RPC("playerManagerSyncPlayers",RPCMode.All,p1,p2,p3,p4);
	}
	[RPC]
	void playerManagerSyncPlayers(bool a, bool b, bool c, bool d){
		p1 = a;
		p2 = b;
		p3 = c;
		p4 = d;
		if(isLoading == true){
			GameObject[] gos = FindObjectsOfType(typeof(GameObject)) as GameObject[];
			foreach(GameObject go in gos)
				go.SendMessage("OnNetworkLoadedLevel1",SendMessageOptions.DontRequireReceiver);
		}
			
	}
	[RPC]
	void playerManagerAddPlayer(int i){
		if(i==1)
			p1 = true;
		if(i==2)
			p2 = true;
		if(i==3)
			p3 = true;
		if(i==4)
			p4 = true;
		pNum += 1;
	}
	public void removePlayer(int i){
		networkView.RPC("playerManagerRemovePlayer",RPCMode.All,i);
	}
	[RPC]
	void playerManagerRemovePlayer(int i){
		if(i==1)
			p1 = false;
		if(i==2)
			p2 = false;
		if(i==3)
			p3 = false;
		if(i==4)
			p4 = false;
		pNum -= 1;
	}
}
